package org.swig.jirr.camera;

import org.swig.jirr.ICameraSceneNode;
import org.swig.jirr.IMeshSceneNode;
import org.swig.jirr.ISceneManager;
import org.swig.jirr.ISceneNode;
import org.swig.jirr.vector3df;

/**
 * @author Dmitry
 */
public class BasicCamera extends IMeshSceneNode {
    private ICameraSceneNode camera;
    private ISceneNode target;

    public BasicCamera(long pointer, boolean own, ISceneManager manager) {
        super(pointer, own);
        initCamera(manager);
    }

    private void initCamera(ISceneManager sceneManager) {
        camera = sceneManager.addCameraSceneNode(this);
        target = sceneManager.addEmptySceneNode();
        target.setPosition(new vector3df(0, 0, 10));
        camera.setParent(this);
        target.setParent(this);
    }

    public void updateCameraTarget() {
        updateAbsolutePosition();
        target.updateAbsolutePosition();
        camera.updateAbsolutePosition();
        camera.setTarget(target.getAbsolutePosition());
    }

    public void setRotationCenter(ISceneNode node) {
        ISceneNode root = getSceneManager().getRootSceneNode();
        vector3df cameraPosition = convertLocalVectorToGlobalSpace(camera.getPosition());
        vector3df targetPosition = convertLocalVectorToGlobalSpace(target.getPosition());
        camera.setParent(root);
        target.setParent(root);

        if (node != null) {
            super.setPosition(node.getPosition());
        } else {
            super.setPosition(new vector3df(0, 0, 0));
        }

        updateAbsolutePosition();
        cameraPosition = convertGlobalVectorToLocalSpace(cameraPosition);
        targetPosition = convertGlobalVectorToLocalSpace(targetPosition);
        camera.setParent(this);
        target.setParent(this);
        camera.setPosition(cameraPosition);
        target.setPosition(targetPosition);
        camera.setTarget(targetPosition);
    }

    public ICameraSceneNode getRealCamera() {
        return camera;
    }

    @Override
    public void moveObjectLocalCoordinates(vector3df direction) {
        target.moveObjectLocalCoordinates(direction);
        camera.moveObjectLocalCoordinates(direction);
    }
}
